SUBROUTINE pastmayorsolution(nn,xx0,fval)
  USE commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'
  
  INTEGER, INTENT(IN)  :: nn
  REAL(8), INTENT(IN)  :: xx0(nn)
  REAL(8), INTENT(OUT) :: fval
  REAL(8)              :: totresl, util, vemax, pcons, ln_relqcons, newsav, emaxp, emaxp1, emaxp2, emaxp3, v_slope, multi_slope
  INTEGER              :: ub_sav, isav

  EXTERNAL utilfun

  totresl = totrest

  !Initialize variables
  util = -infty
  vemax = -infty
  pcons = -infty
  emaxp = -infty

  !Transform xx into relevant variables
  newsav = (totrest*EXP(xx0(1)) + lbound_sav)/(1.0d0 + EXP(xx0(1)))

  !Calculating utility
  pcons = totresl - newsav

  CALL utilfun(pcons,ELNqqt,theta,util)

  IF (pcons <= 0.0d0) THEN
!!$     IF (flag_here == 4) write(*,'(a,4f20.10)') 'pastmyor infty', ELNqqt, pcons, totresl, newsav      
     util = bignumber
     fval = infty
     RETURN
  END IF

  !Calculating the future value function depends on the history of mayors -- really only relevant for iterm =2 (those who have a history)

  !Calculating the emaxp [future value] 
  ln_relqcons = ELNqcons_g

  !Find the point on the saving domain where we need to compute the approximated value function
  DO isav = 1, Nwealth
     IF (newsav <  wealth(isav)) THEN
        ub_sav = isav
        EXIT
     END IF
     ub_sav = Nwealth
  END DO

  !Constructing emaxp for emaxfuntemp and t >= 4 (age enters non-parameterically)
  !Constructing emaxp1: value function if current incumbent will not be audited: audit = 0
  IF (MMg == MMsteal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat1(2) - betat1(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat1(ub_sav) - betat1(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     ! Since they will not be audited, we don't include newsteal and fine1
     emaxp1 = betat1(MMsteal_q-Npol_st-Npol_q-1+1) + betat1(MMsteal_q-Npol_st-Npol_q-1+3)*ln_relqcons + V_slope*(newsav - wealth(ub_sav)) + betat1(ub_sav)

  !Constructing emaxp for emaxfuntemp and t < 4 (age enters parameterically) and for emaxfun
  ELSE IF (MMg == MMage_steal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat1(2) - betat1(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat1(ub_sav) - betat1(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp1 = betat1(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+1) + &
             betat1(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+2)*agemayort + &
             betat1(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+3)*agemayort**2d0 + &
             betat1(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+5)*ln_relqcons + V_slope*(newsav - wealth(ub_sav)) + betat1(ub_sav)

  ELSE
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat1(2) - betat1(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat1(ub_sav) - betat1(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp1 = V_slope*(newsav - wealth(ub_sav)) + betat1(ub_sav)
     
  END IF
  
  !Constructing emaxp2: value function if current incumbent will be audited: audit=1
  IF (MMg == MMsteal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat2(2) - betat2(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat2(ub_sav) - betat2(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     ! Since they will not be audited, we don't include newsteal and fine1
     emaxp2 = betat2(MMsteal_q-Npol_st-Npol_q-1+1) + betat2(MMsteal_q-Npol_st-Npol_q-1+2)*new_fr_stolen_g + betat2(MMsteal_q-Npol_st-Npol_q-1+3)*ln_relqcons + &
          V_slope*(newsav - wealth(ub_sav)) + betat2(ub_sav)

  !Constructing emaxp for emaxfuntemp and t < 4 (age enters parameterically) and for emaxfun
  ELSE IF (MMg == MMage_steal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat2(2) - betat2(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat2(ub_sav) - betat2(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp2 = betat2(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+1) + &
             betat2(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+2)*agemayort + &
             betat2(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+3)*agemayort**2d0 + &
             betat2(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+4)*new_fr_stolen_g + &
             betat2(MMage_steal_q-Npol_age-Npol_st-Npol_q-1+5)*ln_relqcons + V_slope*(newsav - wealth(ub_sav)) + betat2(ub_sav)

  ELSE
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat2(2) - betat2(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat2(ub_sav) - betat2(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp2 = V_slope*(newsav - wealth(ub_sav)) + betat2(ub_sav)
     
  END IF

  emaxp = 0d0
  emaxp = paudit_g*emaxp1 + (1d0-paudit_g)*emaxp2

  ! If flag_no_run == 1 .AND. new_fr_stolen > 0d0 the incumbent cannot run
  IF (flag_no_run == 1 .AND. new_fr_stolen_g > 0d0 .AND. itermi_g < Nnterms .AND. agemayort < Retage -1) THEN

     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat3(2) - betat3(1))/(wealth(2) - wealth(1))
        
     ELSE

        V_slope = (betat3(ub_sav) - betat3(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp3 = V_slope*(newsav - wealth(ub_sav)) + betat3(ub_sav)

     emaxp = 0d0
     emaxp = paudit_g*emaxp1 + (1d0-paudit_g)*emaxp3
     
  END IF

  ! If flag_no_run == 1 .AND. new_fr_stolen > 0d0 the incumbent cannot run
  IF (flag_norun_prob_policy == 1 .AND. new_fr_stolen_g > l_fr_stolen_x .AND. itermi_g < Nnterms .AND. agemayort < Retage -1) THEN

     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))
        V_slope = (1d0+multi_slope)*(betat3(2) - betat3(1))/(wealth(2) - wealth(1))
        
     ELSE

        V_slope = (betat3(ub_sav) - betat3(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp3 = V_slope*(newsav - wealth(ub_sav)) + betat3(ub_sav)

     emaxp = 0d0
     emaxp = paudit_g*emaxp1 + (1d0-paudit_g)*(emaxp2*(1d0 - prob_conv_norun)+emaxp3*prob_conv_norun)
     
  END IF
  
  !Calculating total value [utility+future value]
  vemax = util + betadf*emaxp

  IF(vemax > vemaxoptpmg) THEN
     vemaxoptpmg = vemax
     pconsoptpmg = pcons
     newsavoptpmg = newsav
     utiloptpmg = util
     emaxoptpmg = emaxp
  END IF

  fval = -vemax

!!$  IF (flag_here == 890) write(*,'(a,2i5,7f15.7)') 'pastmyor', MMg, ub_sav, wealth(ub_sav), newsav, pcons, vemaxoptpmg, vemax, util, emaxp
!!$  IF (flag_here == 890) write(*,'(a,15f15.7)') 'beta1', betat1
!!$  IF (flag_here == 890) write(*,'(a,15f15.7)') 'beta2', betat2
  
END SUBROUTINE pastmayorsolution
